package LinkedListPackage;

import org.junit.jupiter.api.Test;

/**
 * @author Lzm
 * @version 1.0
 */
public class SingLinkedListText {
  /*1. 获取节点个数*/
  public static int getNumber(SingleLinkedList l){
    int length = 0;
    if (l.getHeadNode() == null){
      return -1;
    }
    SingleLinkedNode temp = l.getHeadNode();
    while(true){
      if (temp.next == null){
        if (length == 0){
          System.out.println("链表无节点");
        }
        return length;
      }
      temp = temp.next;
      length++;
    }
  }
  /*测试1*/
  @Test
  public void TestMethod1(){
    SingleLinkedList l = new SingleLinkedList();

    SingleLinkedNode n1 = new SingleLinkedNode(1,"1");
    SingleLinkedNode n2 = new SingleLinkedNode(4,"2");
    SingleLinkedNode n3 = new SingleLinkedNode(2,"3");
    SingleLinkedNode n4 = new SingleLinkedNode(10,"3");
    SingleLinkedNode n5 = new SingleLinkedNode(8,"3");
    SingleLinkedNode n6 = new SingleLinkedNode(3,"3");

    l.addByOrder(n1);
    l.addByOrder(n2);
    l.addByOrder(n3);
    l.addByOrder(n4);
    l.addByOrder(n5);
    l.addByOrder(n6);
    // 1 2 3 4 8 10

    System.out.println(SingLinkedListText.getNumber(l));
  }

  /*============================================================*/
  /*2. 获取倒数第k个节点*/
  /*
  * 得到单链表的倒数第index个节点
  * */
  public static SingleLinkedNode findLastSingleNode(SingleLinkedList l, int lastIndex){
    /*判断参数的合法性*/
    if (lastIndex < 1 || lastIndex > SingLinkedListText.getNumber(l)){
      return null;
    }
    /*将倒数第index个换成正数第lastIndex个*/
    int index = SingLinkedListText.getNumber(l) - lastIndex + 1;
    /*循环遍历找到正数第index个节点*/
    SingleLinkedNode temp = l.getHeadNode().next;
    for (int i= 0 ; i < index - 1 ; i++){
      temp = temp.next;
    }
    return temp;
  }

  /*测试2*/
  @Test
  public void TestMethod2(){
    SingleLinkedList l = new SingleLinkedList();

    SingleLinkedNode n1 = new SingleLinkedNode(1,"1");
    SingleLinkedNode n2 = new SingleLinkedNode(4,"2");
    SingleLinkedNode n3 = new SingleLinkedNode(2,"3");
    SingleLinkedNode n4 = new SingleLinkedNode(10,"3");
    SingleLinkedNode n5 = new SingleLinkedNode(8,"3");
    SingleLinkedNode n6 = new SingleLinkedNode(3,"3");

   /* l.addByOrder(n1);
    l.addByOrder(n2);
    l.addByOrder(n3);
    l.addByOrder(n4);
    l.addByOrder(n5);
    l.addByOrder(n6);*/

    System.out.println(SingLinkedListText.findLastSingleNode(l, 6));

  }
  /*============================================================*/
  /*3. 反转链表*/
  public static SingleLinkedNode reverseSingleLinkedList(SingleLinkedList l){
    /*对参数进行合法性判断*/
    if (SingLinkedListText.getNumber(l) <= 1){
      System.out.println("链表不合法");
      return null;
    }

    /*创建一个reverseHead作为反转链表的头*/
    SingleLinkedNode reverseHead = new SingleLinkedNode(0,"");
    /*创建临时辅助节点*/
    SingleLinkedNode cur_next = null;
    SingleLinkedNode cur = l.getHeadNode().next;
    while(true){
      if (cur == null){
        break;
      }
      /*进行反转*/
      /*获得当前节点cur的下一个节点*/
      cur_next = cur.next;
      /*令需要加入reverseHead后一个的节点的next指向当前reverseHead的next*/
      cur.next = reverseHead.next;
      reverseHead.next = cur;
      cur = cur_next;
    }
    l.getHeadNode().next = reverseHead.next;
    return l.getHeadNode();
  }

  /*测试*/
  @Test
  public void TestMethod3(){
    SingleLinkedList l = new SingleLinkedList();

    SingleLinkedNode n1 = new SingleLinkedNode(1,"1");
    SingleLinkedNode n2 = new SingleLinkedNode(4,"2");
    SingleLinkedNode n3 = new SingleLinkedNode(2,"3");
    SingleLinkedNode n4 = new SingleLinkedNode(10,"3");
    SingleLinkedNode n5 = new SingleLinkedNode(8,"3");
    SingleLinkedNode n6 = new SingleLinkedNode(3,"3");

   /* l.addByOrder(n1);
    l.addByOrder(n2);
    l.addByOrder(n3);
    l.addByOrder(n4);
    l.addByOrder(n5);
    l.addByOrder(n6);*/

    System.out.println(SingLinkedListText.reverseSingleLinkedList(l));

  }

  /*4. 逆序打印单链表*/

}
